<?php if (! defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class welcome extends API_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model('account_info_model','maccountInfo');
    $this->accountFields = 'id,ctype,phone,uname,board,position,photo,status,company,concerns,fans,activity,reward,balance,claims,com_name';
    $this->accountInfoFields = 'profile,cprofile,card,thumb,industry,good_industry,interested,single_project,tags,fund_stage,fund_scale,business_type,enterprises';
    $this->load->model('concern_fans_model','mconcern_fans');
    $this->load->model('company_model','mcompany');
  }

  public function index() {
    $vdata = '';
    $where = array();
    $where_ctype = false;
    $ctype_1 = false;
    if(isset($this->data['ctype']) && $this->data['ctype']) {
      $account_ctype = explode(',',$this->data['ctype']);
      $where['in 1'] = array('account.ctype',$account_ctype);
      $where_ctype = true;
      if(count($account_ctype) == 1 && $account_ctype[0] == 1) {
        $ctype_1 = true;
      }
    }
    $interested = false;
    if(isset($this->data['interested']) && $this->data['interested']) {
      $inster = explode(',',$this->data['interested']);
      foreach($inster as $ke=>$va) {
        $par = $ke+5;
        $where['or_like '. ($par+1)] = array('account_info.interested',$va);
        $where['or_like '. ($par+2)] = array('account_info.good_industry',$va);
        $where['or_like '. ($par+3)] = array('account_info.industry',$va);
      }
      $interested = true;

    }
    $kw = isset($this->data['kw']) && $this->data['kw'] ? $this->data['kw'] : false;
    if ($kw) {
      //姓名、公司全称、简称、股票代码、简拼
      $where['or_like 1'] = array('account.uname', $kw);
      $where['or_like 2'] = array('company.name', $kw);
      $where['or_like 3'] = array('company.short', $kw);
      $where['or_like 4'] = array('company.stock_code', $kw);
      $where['or_like 5'] = array('company.initials', $kw);
    }
    $orderby = array('account.activity'=>'desc','account.reg_time'=>'desc');
    if($where_ctype) {
      $account_ctype = explode(',',$this->data['ctype']);
      if(count($account_ctype) == 1 && $account_ctype[0] == 2) {
        $orderby = array('account.sort_id'=>'desc','account.activity'=>'desc');
      } elseif(count($account_ctype) == 1 && $account_ctype[0] == 1) {
          $orderby = array('account.sort_id'=>'desc','account.reg_time'=>'desc');
      }
    }

    // 初始化翻页
    $this->_list();

    $this->Fields = 'account.id,account.photo,account.ctype,account.uname,company.short as com_name,company.short_desc,account.position,account_info.profile,account.status,account.com_short as acom_name,account_info.uid,';
    if($where_ctype) {
      foreach($account_ctype as $val) {
        switch($val) {
          case 1:
            $this->Fields .='account_info.industry,account_info.tags,';
            break;
          case 2:
            $this->Fields .='account_info.interested,account_info.fund_stage,account_info.single_project,';
            break;
          case 3:
            $this->Fields .='account_info.industry,account_info.interested,';
            break;
          case 4:
            $this->Fields .='account_info.good_industry,account_info.interested,';
            break;
          case 5:
            $this->Fields .='account_info.interested,account_info.good_industry,';
            break;
          case 6:
            $this->Fields .='account_info.interested,';
            break;
        }
      }

    } else {
      $this->Fields .='account_info.*,';
    }

    $join = array(
      array('account_info','account_info.uid=account.id','left'),
      array('company','company.id=account.company','left')
    );
    $where['account.status'] = 2;
    $where['account.audit'] = 0;
    $uids = $this->mcompany->get_all(array('uid <>'=>0),'uid');
    $ids = array();
    if($uids) {
      foreach($uids as $row) {
        $ids[] = $row['uid'];
      }
      //$where['in 2'] = array('account.id',$ids);
    }
    //var_dump($where);die();
    if($where_ctype || $kw || $interested) {
      if ($list = $this->macc->get_list_search($this->limit, $this->offset, $orderby, $where, $this->Fields,false,$join)) {
        //echo $this->db->last_query();die();
        foreach($list as $key=>&$row) {
          if($ctype_1) {
            if(!in_array($row['uid'],$ids)) {unset($list[$key]) ;continue;}
          }
          if(($row['ctype'] ==1 && $ids && !$kw && !$interested)) {
            if(!in_array($row['uid'],$ids)) {unset($list[$key]) ;continue;}
          }
          $photo = $row['photo'] ?  one_upload($row['photo']) : '' ;
          $row['photo'] = $photo ? ($photo['thumb'] ? UPLOAD_URL.$photo['thumb'] : UPLOAD_URL.$photo['url']) : '';
          if($row['ctype'] != 1) {
            $row['com_name']  = $row['com_name'] ? $row['com_name'] : $row['acom_name'];
          }
          if($this->userinfo) {
            $fan = $this->mconcern_fans->get_one(array('uid'=>$this->userinfo['id'],'rid'=>$row['uid']),'is_deleted');
            //echo ($this->db->last_query());die();
            $row['is_friend'] = ($fan && $fan['is_deleted']) ? 1 : 0;
          } else {
            $row['is_friend'] = 0;
          }
          //var_dump($row);
          //$row['is_friend'] = $this->userinfo ? '' : 0;
          $row['profile'] = $row['ctype'] == 1 ? $row['short_desc'] : '';
          switch($row['ctype']) {
            case 1:
              $row['industry'] = get_mcategory_titles($row['industry'],2);
              $row['tags'] = get_mcategory_titles($row['tags'],2);
              break;
            case 2:
              $row['interested'] = get_mcategory_titles($row['interested'],4);
              $row['fund_stage'] = get_mcategory_titles($row['fund_stage'],3);
              $row['single_project'] = get_mcategory_titles($row['single_project']);
              break;
            case 3:
              $row['industry'] = get_mcategory_titles($row['industry'],4);
              $row['interested'] = get_mcategory_titles($row['interested'],4);
              break;
            case 4:
              $row['good_industry'] = get_mcategory_titles($row['good_industry']);
              $row['interested'] = get_mcategory_titles($row['interested'],4);
              break;
            case 5:
              $row['good_industry'] = get_mcategory_titles($row['good_industry']);
              $row['interested'] = get_mcategory_titles($row['interested'],4);
              break;
            case 6:
              //$info_data['business_type'] = get_mcategory_titles($info_data['business_type']);
              $row['interested'] = get_mcategory_titles($row['interested'],4);
              break;
          }
        }
        //photo2url($list);
        //$this->mproduct->get_count_all($where);
        $this->vdata['returnCode'] = '200';
        $this->vdata['returnInfo'] = '操作成功';
        $this->vdata['secure'] = JSON_SECURE;
        $this->vdata['content'] = array_values($list);
      } else {
        $this->vdata['returnCode'] = '200';
        $this->vdata['returnInfo'] = '无数据';
        $this->vdata['content'] = '';
      }
    } else {
      $this->vdata['returnCode'] = '10002';
      $this->vdata['returnInfo'] = '请输入搜索内容';
      $this->vdata['content'] = '';
    }

    // 返回json数据
    $this->_send_json($this->vdata);
  }
}
